guard
, when
, and unless
allow for conditional operations and short-cutting in monadic expressions.
Guards
Guards are used to stop the monadic expression continuing if a flag is true
(for guard
) or false
(for guardnot
).
They only work with monads that have an 'alternative value' (which is usually used as the error condition: Left
in
Either
for example). An alternative value is provided when the guard triggers:
from x in ma
from _ in guard(x == 100, Error.New("x should be 100"))
select x;
Supported monads are:
Either
EitherUnsafe
EitherAsync
Fin
Validation
Aff
Eff
When and Unless
when
and unless
are similar to guards, but instead of providing the alternative value, you provide an alternative monad
to run. This monad could be in a failed state, or it could run a successful side effect (an Aff
calling Console<RT>.writeLine()
for example).
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
- Prelude
- when <F> (bool flag, K<F, Unit> alternative)
- unless <F> (bool flag, K<F, Unit> alternative)
- guard <F> (bool flag)
- guard <E> (bool flag, Func<E> False)
- guard <E> (bool flag, E False)
- guard (bool flag, Func<Error> False)
- guard (bool flag, Error False)
- guardnot <E> (bool flag, Func<E> True)
- guardnot <E> (bool flag, E True)
- guardnot (bool flag, Func<Error> True)
- guardnot (bool flag, Error True)
method K<F, Unit> when <F> (bool flag, K<F, Unit> alternative) Source #
Conditional execution of Applicative
expressions
Run the alternative
when the flag
is true
, return pure ()
when false
param | flag | If |
param | alternative | Computation to run if the |
returns | Either the result of the |
from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;
method K<F, Unit> unless <F> (bool flag, K<F, Unit> alternative) Source #
Conditional execution of Applicative
expressions
Run the alternative
when the flag
is false
, return pure ()
when true
param | flag | If |
param | alternative | Computation to run if the flag is |
returns | Either the result of the |
from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;
method K<F, Unit> guard <F> (bool flag) Source #
Guard against continuing an applicative expression
param | flag | Flag for continuing |
returns | Applicative that yields |
method Guard<E, Unit> guard <E> (bool flag, Func<E> False) Source #
Guard against continuing a monadic expression
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<E, Unit> guard <E> (bool flag, E False) Source #
Guard against continuing a monadic expression
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<Error, Unit> guard (bool flag, Func<Error> False) Source #
Guard against continuing a monadic expression
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<Error, Unit> guard (bool flag, Error False) Source #
Guard against continuing a monadic expression
param | flag | Flag for continuing |
param | False | If the flag is false, this provides the error |
returns | Guard |
method Guard<E, Unit> guardnot <E> (bool flag, Func<E> True) Source #
Guard against continuing a monadic expression
param | flag | Flag for continuing |
param | True | If the flag is false, this provides the error |
returns | Guard |
method Guard<E, Unit> guardnot <E> (bool flag, E True) Source #
Guard against continuing a monadic expression
param | flag | Flag for continuing |
param | True | If the flag is false, this provides the error |
returns | Guard |